/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.InstrumentoDao;
import edu.upc.clase.demo.entity.Instrumento;
import edu.upc.clase.demo.entity.Usuario;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *
    private String codInst;
    private String modeloInst;
    private String caracInst;
    private Integer anioInst;
    public boolean basicInst;
    private String marcaInst;
    private String tipoInst;
 * @author usuario
 */
@Repository
public class InstrumentoDaoImpl extends SimpleJdbcDaoSupport implements InstrumentoDao{
  private static Logger log = LoggerFactory.getLogger(InstrumentoDaoImpl.class);
  
      @Autowired
       public InstrumentoDaoImpl (DataSource dataSource) {
       log.info("Asignando el dataSource");
       setDataSource(dataSource);    
    }
      @Override
      public Integer insertar(Instrumento instrumento) {
        getJdbcTemplate().update(
                "insert into Instrumento (codInst,modeloInst,caracInst,anioInst,"
                + "basicInst,marcaInst,tipoInst) values (?, ?, ?, ?, ?, ?, ?)",
                instrumento.getCodInst(),instrumento.getModeloInst(),instrumento.getCaracInst(),
                instrumento.getAnioInst(),instrumento.isBasicInst(),instrumento.getMarcaInst(),
                instrumento.getTipoInst());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_codInst()");
    }
      @Override
      public void actualizar(Instrumento instrumento) {
        getJdbcTemplate().update(
                "update Instrumento set modeloInst = ?,caracInst = ?,anioInst = ?,"
                + "basicInst = ?,marcaInst = ?,tipoInst = ? where codInst = ?",
                instrumento.getModeloInst(),instrumento.getCaracInst(),
                instrumento.getAnioInst(),instrumento.isBasicInst(),instrumento.getMarcaInst(),
                instrumento.getTipoInst(),instrumento.getCodInst());
    }

    @Override
    public void eliminar(Instrumento instrumento) {
        getJdbcTemplate().update(
                "delete from Instrumento where codInst = ?",instrumento.getCodInst() );
    }
    
    @Override
    public Instrumento buscar(String codInst) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select codInst,modeloInst,caracInst,anioInst,"
                    + "basicInst,marcaInst,tipoInst from Local where tipoInst=?",
                    new BeanPropertyRowMapper<Instrumento>(Instrumento.class), codInst);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Instrumento> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select  codInst,modeloInst,caracInst,anioInst,"
                + "basicInst,marcaInst,tipoInst from Instrumento",
                new BeanPropertyRowMapper<Instrumento>(Instrumento.class));
    }

    @Override
    public List<Instrumento> buscarPorNombre(String CaracInst) {
        try {
            Map<String,String> parametros = new HashMap<String,String>();
            parametros.put("caracInst","%"+CaracInst+"%");
            return getSimpleJdbcTemplate().query(
                    "select * from Instrumentos where CaracInst like :CaracInst",
                    new BeanPropertyRowMapper<Instrumento>(Instrumento.class),parametros);
        } catch (EmptyResultDataAccessException e) {
            return null;
             }
    }
}